package net.apps.std;

import net.apps.core.Message;
import net.apps.core.Protocol;
import net.apps.core.meta.AMessage;
import net.apps.core.meta.Meta;

public class AbsProtocol implements Protocol {

	protected final Meta meta_of_protocol;

	public AbsProtocol(Meta meta) {
		this.meta_of_protocol = meta;
	}

	@Override
	public final Meta getProtocolMeta() {
		return meta_of_protocol;
	}
	
	protected abstract class AbsMessage implements Message {
		private final Meta message_of_meta;
		protected AbsMessage() {
			AMessage ann = getClass().getAnnotation(AMessage.class);
			this.message_of_meta = AbsProtocol.this.meta_of_protocol.getTypeDecl(ann.id());
		}
		protected AbsMessage(int id) {
			assert (getClass().getAnnotation(AMessage.class).id() == id);
			this.message_of_meta = AbsProtocol.this.meta_of_protocol.getTypeDecl(id);
		}
		@Override
		public final Meta getMessageMeta() {
			return message_of_meta;
		}
	}

}
